/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.client.ssh;

import java.util.HashMap;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

import sshtunneler.library.Server;

public class SSHUserDataContainer {
	/************ SINGLETON ****************/
	public static SSHUserDataContainer inst = new SSHUserDataContainer();

	private SSHUserDataContainer() {
		this.passwords = new HashMap<Server, String>();
	}

	/************** INST *******************/
	private HashMap<Server, String> passwords;

	public String getPassword(Server connection) {
		if (this.passwords.containsKey(connection)) {
			return this.passwords.get(connection);
		}

		return null;
	}

	public boolean promptYesNo(Server connection, String str) {
		return JOptionPane.showConfirmDialog(null, str) == JOptionPane.OK_OPTION;
	}

	public String getPassphrase(Server connection) {
		return null;
	}

	public boolean promptPassphrase(Server connection, String message) {
		return true;
	}

	public boolean promptPassword(Server connection, String message) {
		JLabel label = new JLabel(message);
		JPasswordField jpf = new JPasswordField();
		int ret = JOptionPane.showConfirmDialog(null,
				new Object[] { label, jpf }, "Password ...",
				JOptionPane.OK_CANCEL_OPTION);
		if (ret == JOptionPane.OK_OPTION) {
			this.passwords.put(connection, String.valueOf(jpf.getPassword()));
			return true;
		} else {
			return false;
		}
	}

	public void showMessage(Server connection, String message) {
		JOptionPane.showMessageDialog(null, message);
	}
}
